package com.xt.wikiperfumapi.Controller.Mail;


import com.xt.wikiperfumapi.model.UserInfo;
import com.xt.wikiperfumapi.model.vo.RestBean;
import com.xt.wikiperfumapi.service.impl.UserInfoServiceImpl;
import com.xt.wikiperfumapi.service.mail.impl.EmailServiceImpl;
import com.xt.wikiperfumapi.util.JedisUtil;
import com.xt.wikiperfumapi.util.RandomCodeGenerator;
import jakarta.annotation.Resource;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 24862
 */
@RestController
@RequestMapping("/api/mail")
@Slf4j
public class SendMailController {

    @Resource
    EmailServiceImpl emailService;

    @Resource
    RandomCodeGenerator generator;

    @Resource
    JedisUtil jedisUtil;

    @Resource
    UserInfoServiceImpl userLoginInfoService;

    @Value("${location.port}")
    private String localAddress;

    @GetMapping("/auth")
    public RestBean sendEmails(@RequestParam("email") String email) {
        String code = generator.generateRandomCode(6);
        try {
            //进行redis进行验证码缓存
            Map<String, Long> emailTTl = new HashMap<>();
            if (jedisUtil.exist(6, email)) {
                log.info(email + "该账号已经发送了邮件" + jedisUtil.getValueTTL(6, email));
                emailTTl.put(email, jedisUtil.getValueTTL(6, email));
                return RestBean.failure(403, emailTTl);
            }
            jedisUtil.setNewValExpire(6, email, code, 300);//验证码生存时间为5分钟
            //采用redis进行缓存验证码
            emailService.sendMessageToEmail(code, email);
            emailTTl.put(email, jedisUtil.getValueTTL(6, email));
            return RestBean.success(emailTTl);//返回邮箱和秒数
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.failure(500, "发送失败");
        }
    }


    @GetMapping("/forget")
    public RestBean forgetEmails(@RequestParam("username") String username) {
        String code = generator.generateRandomCode(6);
        try {
            //进行redis进行验证码缓存
            Map<String, Long> emailTTl = new HashMap<>();
            List<UserInfo> list = userLoginInfoService.list();
            String email = new String();
            for (UserInfo u : list) {
                if (u.getUsername().equals(username)) {
                    email = u.getMail();
                }
            }
            if (email.isEmpty()) {
                return RestBean.failure(403, "没有该用户");
            }
            if (jedisUtil.exist(4, email)) {
                log.info(email + "该账号已经发送了邮件" + jedisUtil.getValueTTL(4, email));
                emailTTl.put(email, jedisUtil.getValueTTL(4, email));
                return RestBean.failure(403, emailTTl);
            }
            jedisUtil.setNewValExpire(4, email, code, 300);//验证码生存时间为5分钟
            //采用redis进行缓存验证码
            emailService.sendMessageToEmail(code, email);
            emailTTl.put(email, jedisUtil.getValueTTL(4, email));
            return RestBean.success(emailTTl);//返回邮箱和秒数
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.failure(500, "发送失败");
        }
    }
    @GetMapping("/security")
    public RestBean securityEmails(@RequestParam("email") String email) {
        String code = generator.generateRandomCode(6);
        try {
            //进行redis进行验证码缓存
            Map<String, Long> emailTTl = new HashMap<>();
            if (email.isEmpty()) {
                return RestBean.failure(403, "没有该用户");
            }
            if (jedisUtil.exist(5, email)) {
                log.info(email + "该账号已经发送了邮件" + jedisUtil.getValueTTL(5, email));
                emailTTl.put(email, jedisUtil.getValueTTL(5, email));
                return RestBean.failure(403, emailTTl);
            }
            jedisUtil.setNewValExpire(5, email, code, 300);//验证码生存时间为5分钟
            //采用redis进行缓存验证码
            emailService.sendMessageToEmail(code, email);
            emailTTl.put(email, jedisUtil.getValueTTL(5, email));
            return RestBean.success(emailTTl);//返回邮箱和秒数
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.failure(500, "发送失败");
        }
    }
    @GetMapping("/initPassword")
    public RestBean forgetPassword(@RequestParam("email") String email) {
        String code = generator.generateRandomCode(21);
        try {
            String  urlLink = localAddress+"/api/auth/initPassword?email="+email+"&wikicode="+code;
            String jLink = localAddress+"/api/auth/initPassword/1?email="+email+"&wikicode="+code;
            String zlink = localAddress+"/api/auth/initPassword/2?email="+email+"&wikicode="+code;
            String klink = localAddress+"/api/auth/initPassword/3?email="+email+"&wikicode="+code;
            //进行redis进行验证码缓存
            Map<String, Long> emailTTl = new HashMap<>();
            if (email.isEmpty()) {
                return RestBean.failure(403, "没有该用户");
            }
            if (jedisUtil.exist(3, email)) {
                log.info(email + "该账号已经发送了邮件" + jedisUtil.getValueTTL(3, email));
                emailTTl.put(email, jedisUtil.getValueTTL(3, email));
                return RestBean.failure(403, emailTTl);
            }
            jedisUtil.setNewValExpire(3, email, urlLink, 300);//验证码生存时间为5分钟
            //采用redis进行缓存验证码
            Date date = new Date();
            Date date1 =new Date();
            date1.setMinutes(date.getMinutes() + 5);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            emailService.sendMessageEmail("Wiki账号重置密码邮箱",
                    "点击下方链接即可重置账号，初始账号为123456<br/>" +
                            "当前时间为" + sdf.format(date) +
                            "<br/><a href="+jLink+">重置账号密码强度低</a><br/>" +
                            "<br/><a href="+zlink+">重置账号密码强度中等</a><br/>" +
                            "<br/><a href="+klink+">重置账号密码强度困难</a><br/>" +
                            "<br/>过期时间之前可以通过刷新重新获取新的随机密码<br/>" +
                            "过期时间为"+ sdf.format(date1),"wiki用户",email
                    );
//            emailService.sendMessageToEmail(code, email);
            emailTTl.put(email, jedisUtil.getValueTTL(3, email));
            return RestBean.success(emailTTl);//返回邮箱和秒数
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.failure(500, "发送失败");
        }
    }



}
